﻿using System;
using System.Collections.Generic;
using System.Text;
using Kugar.Core.ExtMethod;

namespace Kugar.Core.Communications.Transfer
{
    public static class TransferManager
    {
        private static Lazy<TCPTransferFactory> tcpFactory = null;
        private static Lazy<UDPFactory> udpFactory = null;
        private static Lazy<RUDPFactory> rudpFactory = null;
        private static Lazy<SerialPortTransferFactory> serialPortFactory = null;

        private static Dictionary<IAsyncResult,ITransferFactory> _cacheConnectFactory=new Dictionary<IAsyncResult, ITransferFactory>();
        private static object lockerObj=new object();

        public static void Listen(TransferConnectConfig config)
        {
            var factory = getFactory(config);

            if (factory!=null)
            {
                factory.Listen(config);
            }
        }

        public static bool ConnectToAsync(TransferAsyncEventArgs ea,TransferConnectConfig config)
        {
            var factory = getFactory(config);

            if (factory==null)
            {
                ea.Error = TransferExceptionHelper.GetTransferException("无法获取指定配置相应的连接类");
                return false;
            }
            else
            {
                return factory.ConnectToAsync(ea, config);
            }

            
        }

        //public static IAsyncResult BeginConnectTo(TransferConnectConfig config,AsyncCallback callback,object state)
        //{
        //    var factory = getFactory(config);

        //    var iar=factory.BeginConnectTo(config,callback,state);

        //    lock (lockerObj)
        //    {
        //        _cacheConnectFactory.Add(iar,factory);
        //    }

        //    return iar;
        //}

        //public static ITransfer EndConnectTo(IAsyncResult iar)
        //{
        //    ITransfer transfer = null;
        //    Exception error=null;

        //    transfer = EndConnectTo(iar, ref error);

        //    if (error!=null)
        //    {
        //        throw error;
        //    }
        //    else
        //    {
        //        return transfer;
        //    }
        //}

        //public static ITransfer EndConnectTo(IAsyncResult iar,ref Exception error)
        //{
        //    ITransferFactory factory = null;

        //    lock (lockerObj)
        //    {
        //        if (_cacheConnectFactory.TryGetValue(iar,out factory))
        //        {
        //            _cacheConnectFactory.Remove(iar);
        //        }

        //        //factory = _cacheConnectFactory.TryGetValue(iar);
                
        //    }

        //    if (factory!=null)
        //    {
        //        return factory.EndConnectTo(iar, ref error);
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}

        

        private static ITransferFactory getFactory(TransferConnectConfig config)
        {
            switch (config.Protocol)
            {
                case TransferProtocolType.TCP_Normal:
                case TransferProtocolType.TCP_SpliteBag:
                    //var tcpConfig = (SocketManager_ListenTCPPortConfig)listenConfig;
                    return tcpFactory.Value;
                    break;

                case TransferProtocolType.UDP_Normal:
                    //var udpConfig = (SocketManager_ListenUDPPortConfig)listenConfig;
                    return udpFactory.Value;
                    break;

                case TransferProtocolType.UDP_RUDP:
                    return rudpFactory.Value;
                    break;

                case TransferProtocolType.SerialPort:
                    return serialPortFactory.Value;
                    break;
            }

            return null;
        }
    }
}
